﻿using ShenNius.Blog.Common;
using ShenNius.Blog.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ShenNius.Blog.Models;

namespace ShenNius.Blog.ViewComponents
{
    public class RightViewComponent : ViewComponent
    {
        private readonly IOptionsMonitor<SiteConfig> _siteConfig;
        public HttpHelper _httpHelper { get; set; }
        public RightViewComponent(HttpHelper httpHelper, IOptionsMonitor<SiteConfig> siteConfig)
        {
            _httpHelper = httpHelper;
            this._siteConfig = siteConfig;
        }
        public async Task<IViewComponentResult> InvokeAsync(string columnParentSpell = "blog", bool isIndex = false)
        {
            var model = await _httpHelper.GetAsync<ApiResult<RightVM>>($"/api/index/get-right-data?spell={columnParentSpell}&&siteId={_siteConfig.CurrentValue.GlobalSiteId}");
            var listTags = await _httpHelper.GetAsync<ApiResult<List<string>>>($"/api/index/get-all-tags?spell={columnParentSpell}&&siteId={_siteConfig.CurrentValue.GlobalSiteId}");
            model.Data.IsIndex = isIndex;
            if (listTags?.Data == null)
            {
                model.Data.AllTags = new List<string>();
            }
            else {
                model.Data.AllTags = listTags?.Data;
            }           
            return View(model?.Data);
        }
    }
}
